home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / devel / lang / compiler / byacc.lzh / byacc / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-29  |  6.2 KB  |  363 lines

  1. #include <signal.h>
  2. #include "defs.h"
  3.  
  4. char dflag;
  5. char lflag;
  6. char rflag;
  7. char tflag;
  8. char vflag;
  9.  
  10. char *file_prefix = "y";
  11. char *myname = "yacc";
  12. char *temp_form = "yacc.XXXXXXX";
  13.  
  14. int lineno;
  15. int outline;
  16.  
  17. char *action_file_name;
  18. char *code_file_name;
  19. char *defines_file_name;
  20. char *input_file_name = "";
  21. char *output_file_name;
  22. char *text_file_name;
  23. char *union_file_name;
  24. char *verbose_file_name;
  25.  
  26. FILE *action_file;    /*  a temp file, used to save actions associated    */
  27.             /*  with rules until the parser is written        */
  28. FILE *code_file;    /*  y.code.c (used when the -r option is specified) */
  29. FILE *defines_file;    /*  y.tab.h                        */
  30. FILE *input_file;    /*  the input file                    */
  31. FILE *output_file;    /*  y.tab.c                        */
  32. FILE *text_file;    /*  a temp file, used to save text until all        */
  33.             /*  symbols have been defined                */
  34. FILE *union_file;    /*  a temp file, used to save the union            */
  35.             /*  definition until all symbol have been        */
  36.             /*  defined                        */
  37. FILE *verbose_file;    /*  y.output                        */
  38.  
  39. int nitems;
  40. int nrules;
  41. int nsyms;
  42. int ntokens;
  43. int nvars;
  44.  
  45. int   start_symbol;
  46. char  **symbol_name;
  47. short *symbol_value;
  48. short *symbol_prec;
  49. char  *symbol_assoc;
  50.  
  51. short *ritem;
  52. short *rlhs;
  53. short *rrhs;
  54. short *rprec;
  55. char  *rassoc;
  56. short **derives;
  57. char *nullable;
  58.  
  59. extern char *mktemp();
  60. extern char *getenv();
  61.  
  62.  
  63. void
  64. done(k)
  65. int k;
  66. {
  67.     if (action_file) { fclose(action_file); unlink(action_file_name); }
  68.     if (text_file) { fclose(text_file); unlink(text_file_name); }
  69.     if (union_file) { fclose(union_file); unlink(union_file_name); }
  70.     exit(k);
  71. }
  72.  
  73.  
  74. void
  75. onintr()
  76. {
  77.     done(1);
  78. }
  79.  
  80.  
  81. void
  82. set_signals()
  83. {
  84.     if (signal(SIGINT, SIG_IGN) != SIG_IGN)
  85.     signal(SIGINT, onintr);
  86.     if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
  87.     signal(SIGTERM, onintr);
  88.     if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
  89.     signal(SIGHUP, onintr);
  90. }
  91.  
  92.  
  93. usage()
  94. {
  95.     fprintf(stderr, "usage: %s [-dlrtv] [-b file_prefix] filename\n", myname);
  96.     exit(1);
  97. }
  98.  
  99.  
  100. getargs(argc, argv)
  101. int argc;
  102. char *argv[];
  103. {
  104.     register int i;
  105.     register char *s;
  106.  
  107.     if (argc > 0) myname = argv[0];
  108.     for (i = 1; i < argc; ++i)
  109.     {
  110.     s = argv[i];
  111.     if (*s != '-') break;
  112.     switch (*++s)
  113.     {
  114.     case '\0':
  115.         input_file = stdin;
  116.         if (i + 1 < argc) usage();
  117.         return;
  118.  
  119.     case '-':
  120.         ++i;
  121.         goto no_more_options;
  122.  
  123.     case 'b':
  124.         if (*++s)
  125.          file_prefix = s;
  126.         else if (++i < argc)
  127.         file_prefix = argv[i];
  128.         else
  129.         usage();
  130.         continue;
  131.  
  132.     case 'd':
  133.         dflag = 1;
  134.         break;
  135.  
  136.     case 'l':
  137.         lflag = 1;
  138.         break;
  139.  
  140.     case 'r':
  141.         rflag = 1;
  142.         break;
  143.  
  144.     case 't':
  145.         tflag = 1;
  146.         break;
  147.  
  148.     case 'v':
  149.         vflag = 1;
  150.         break;
  151.  
  152.     default:
  153.         usage();
  154.     }
  155.  
  156.     for (;;)
  157.     {
  158.         switch (*++s)
  159.         {
  160.         case '\0':
  161.         goto end_of_option;
  162.  
  163.         case 'd':
  164.         dflag = 1;
  165.         break;
  166.  
  167.         case 'l':
  168.         lflag = 1;
  169.         break;
  170.  
  171.         case 'r':
  172.         rflag = 1;
  173.         break;
  174.  
  175.         case 't':
  176.         tflag = 1;
  177.         break;
  178.  
  179.         case 'v':
  180.         vflag = 1;
  181.         break;
  182.  
  183.         default:
  184.         usage();
  185.         }
  186.     }
  187. end_of_option:;
  188.     }
  189.  
  190. no_more_options:;
  191.     if (i + 1 != argc) usage();
  192.     input_file_name = argv[i];
  193. }
  194.  
  195.  
  196. char *
  197. allocate(n)
  198. unsigned n;
  199. {
  200.     register char *p;
  201.  
  202.     p = NULL;
  203.     if (n)
  204.     {
  205.     p = CALLOC(1, n);
  206.     if (!p) no_space();
  207.     }
  208.     return (p);
  209. }
  210.  
  211.  
  212. create_file_names()
  213. {
  214.     int i, len;
  215.     char *tmpdir;
  216.  
  217.     tmpdir = getenv("TMPDIR");
  218.     if (tmpdir == 0) tmpdir = "/tmp";
  219.  
  220.     len = strlen(tmpdir);
  221.     i = len + 13;
  222.     if (len && tmpdir[len-1] != '/')
  223.     ++i;
  224.  
  225.     action_file_name = MALLOC(i);
  226.     if (action_file_name == 0) no_space();
  227.     text_file_name = MALLOC(i);
  228.     if (text_file_name == 0) no_space();
  229.     union_file_name = MALLOC(i);
  230.     if (union_file_name == 0) no_space();
  231.  
  232.     strcpy(action_file_name, tmpdir);
  233.     strcpy(text_file_name, tmpdir);
  234.     strcpy(union_file_name, tmpdir);
  235.  
  236.     if (len && tmpdir[len - 1] != '/')
  237.     {
  238.     action_file_name[len] = '/';
  239.     text_file_name[len] = '/';
  240.     union_file_name[len] = '/';
  241.     ++len;
  242.     }
  243.  
  244.     strcpy(action_file_name + len, temp_form);
  245.     strcpy(text_file_name + len, temp_form);
  246.     strcpy(union_file_name + len, temp_form);
  247.  
  248.     action_file_name[len + 5] = 'a';
  249.     text_file_name[len + 5] = 't';
  250.     union_file_name[len + 5] = 'u';
  251.  
  252.     mktemp(action_file_name);
  253.     mktemp(text_file_name);
  254.     mktemp(union_file_name);
  255.  
  256.     len = strlen(file_prefix);
  257.  
  258.     output_file_name = MALLOC(len + 7);
  259.     if (output_file_name == 0)
  260.     no_space();
  261.     strcpy(output_file_name, file_prefix);
  262.     strcpy(output_file_name + len, OUTPUT_SUFFIX);
  263.  
  264.     if (rflag)
  265.     {
  266.     code_file_name = MALLOC(len + 8);
  267.     if (code_file_name == 0)
  268.         no_space();
  269.     strcpy(code_file_name, file_prefix);
  270.     strcpy(code_file_name + len, CODE_SUFFIX);
  271.     }
  272.     else
  273.     code_file_name = output_file_name;
  274.  
  275.     if (dflag)
  276.     {
  277.     defines_file_name = MALLOC(len + 7);
  278.     if (defines_file_name == 0)
  279.         no_space();
  280.     strcpy(defines_file_name, file_prefix);
  281.     strcpy(defines_file_name + len, DEFINES_SUFFIX);
  282.     }
  283.  
  284.     if (vflag)
  285.     {
  286.     verbose_file_name = MALLOC(len + 8);
  287.     if (verbose_file_name == 0)
  288.         no_space();
  289.     strcpy(verbose_file_name, file_prefix);
  290.     strcpy(verbose_file_name + len, VERBOSE_SUFFIX);
  291.     }
  292. }
  293.  
  294.  
  295. open_files()
  296. {
  297.     create_file_names();
  298.  
  299.     if (input_file == 0)
  300.     {
  301.     input_file = fopen(input_file_name, "r");
  302.     if (input_file == 0)
  303.         open_error(input_file_name);
  304.     }
  305.  
  306.     action_file = fopen(action_file_name, "w");
  307.     if (action_file == 0)
  308.     open_error(action_file_name);
  309.  
  310.     text_file = fopen(text_file_name, "w");
  311.     if (text_file == 0)
  312.     open_error(text_file_name);
  313.  
  314.     if (vflag)
  315.     {
  316.     verbose_file = fopen(verbose_file_name, "w");
  317.     if (verbose_file == 0)
  318.         open_error(verbose_file_name);
  319.     }
  320.  
  321.     if (dflag)
  322.     {
  323.     defines_file = fopen(defines_file_name, "w");
  324.     if (defines_file == 0)
  325.         open_error(defines_file_name);
  326.     union_file = fopen(union_file_name, "w");
  327.     if (union_file ==  0)
  328.         open_error(union_file_name);
  329.     }
  330.  
  331.     output_file = fopen(output_file_name, "w");
  332.     if (output_file == 0)
  333.     open_error(output_file_name);
  334.  
  335.     if (rflag)
  336.     {
  337.     code_file = fopen(code_file_name, "w");
  338.     if (code_file == 0)
  339.         open_error(code_file_name);
  340.     }
  341.     else
  342.     code_file = output_file;
  343. }
  344.  
  345.  
  346. int
  347. main(argc, argv)
  348. int argc;
  349. char *argv[];
  350. {
  351.     set_signals();
  352.     getargs(argc, argv);
  353.     open_files();
  354.     reader();
  355.     lr0();
  356.     lalr();
  357.     make_parser();
  358.     verbose();
  359.     output();
  360.     done(0);
  361.     /*NOTREACHED*/
  362. }
  363.